﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using l1cs.server.datatables;
using l1cs.server.model;

namespace l1cs.server.clientpackets
{
    public class C_FishClick : ClientBasePacket
    {
        private const String _C_FISHCLICK = "[C] C_FishClick";
        private static Logger _log = Logger.getLogger( typeof( C_FishClick ).FullName );
        private static Random _random = new Random();

        private static int[] HEADING_TABLE_X = { 0, 1, 1, 1, 0, -1, -1, -1 };
        private static int[] HEADING_TABLE_Y = { -1, -1, 0, 1, 1, 1, 0, -1 };

        public C_FishClick( byte[] abyte0, ClientThread clientthread )
            : base( abyte0 )
        {
            L1PcInstance pc = clientthread.getActiveChar();
            long currentTime = DateTime.Now.Ticks / 10000;
            long time = pc.getFishingTime();

            if ( currentTime < ( time + 500 ) && currentTime > ( time - 500 ) && pc.isFishingReady() )
            {
                finishFishing( pc );

                int chance = _random.Next( 200 ) + 1;
                if ( chance < 50 )
                {
                    successFishing( pc, 41298, "$5256" ); // 25%
                }
                else if ( chance < 65 )
                {
                    successFishing( pc, 41300, "$5258" ); // 7.5%
                }
                else if ( chance < 80 )
                {
                    successFishing( pc, 41299, "$5257" ); // 7.5%
                }
                else if ( chance < 90 )
                {
                    successFishing( pc, 41296, "$5249" ); // 5%
                }
                else if ( chance < 100 )
                {
                    successFishing( pc, 41297, "$5250" ); // 5%
                }
                else if ( chance < 105 )
                {
                    successFishing( pc, 41301, "$5259" ); // 2.5%
                }
                else if ( chance < 110 )
                {
                    successFishing( pc, 41302, "$5260" ); // 2.5%
                }
                else if ( chance < 115 )
                {
                    successFishing( pc, 41303, "$5261" ); // 2.5%
                }
                else if ( chance < 120 )
                {
                    successFishing( pc, 41304, "$5262" ); // 2.5%
                }
                else if ( chance < 123 )
                {
                    successFishing( pc, 41306, "$5263" ); // 1.5%
                }
                else if ( chance < 126 )
                {
                    successFishing( pc, 41307, "$5265" ); // 1.5%
                }
                else if ( chance < 129 )
                {
                    successFishing( pc, 41305, "$5264" ); // 1.5%
                }
                else if ( chance < 134 )
                {
                    successFishing( pc, 21051, "$5269" ); // 2.5%
                }
                else if ( chance < 139 )
                {
                    successFishing( pc, 21052, "$5270" ); // 2.5%
                }
                else if ( chance < 144 )
                {
                    successFishing( pc, 21053, "$5271" ); // 2.5%
                }
                else if ( chance < 159 )
                {
                    successFishing( pc, 21054, "$5272" ); // 2.5%
                }
                else if ( chance < 164 )
                {
                    successFishing( pc, 21055, "$5273" ); // 2.5%
                }
                else if ( chance < 169 )
                {
                    successFishing( pc, 21056, "$5274" ); // 2.5%
                }
                else if ( chance < 171 )
                {
                    successFishing( pc, 41252, "$5248" ); // 1.0%
                }
                else
                {
                    pc.sendPackets( new S_ServerMessage( 1136, "" ) ); // 釣りに失敗しました。
                }
            }
            else
            {
                finishFishing( pc );
                pc.sendPackets( new S_ServerMessage( 1136, "" ) ); // 釣りに失敗しました。
            }
        }

        private void finishFishing( L1PcInstance pc )
        {
            pc.setFishingTime( 0 );
            pc.setFishingReady( false );
            pc.setFishing( false );
            pc.sendPackets( new S_CharVisualUpdate( pc ) );
            pc.broadcastPacket( new S_CharVisualUpdate( pc ) );
            FishingTimeController.getInstance().removeMember( pc );
        }

        private void successFishing( L1PcInstance pc, int itemId, String message )
        {
            L1ItemInstance item = ItemTable.getInstance().createItem( itemId );
            item.startItemOwnerTimer( pc );
            int heading = pc.getHeading();
            int[] loc = { pc.getX(), pc.getY() };
            int[] dropLoc = new int[2];

            dropLoc[0] = loc[0] - HEADING_TABLE_X[heading];
            dropLoc[1] = loc[1] - HEADING_TABLE_Y[heading];

            if ( pc.getMap().isPassable( dropLoc[0], dropLoc[1] ) )
            {
                L1World.getInstance().getInventory( dropLoc[0], dropLoc[1], pc.getMapId() ).storeItem( item );
            }
            else
            {
                L1World.getInstance().getInventory( loc[0], loc[1], pc.getMapId() ).storeItem( item );
            }
            pc.sendPackets( new S_ServerMessage( 1185, message ) ); // 釣りに成功して%0%oを釣りました。
        }

        override
        public String getType()
        {
            return _C_FISHCLICK;
        }
    }
}
